/**
 *
 */
package com.handinfo.uimenu.window;

import java.io.DataInputStream;
import java.io.IOException;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;

import android.app.AlertDialog;
import android.util.Log;

import com.handinfo.engine.AbstractGameScreen;
import com.handinfo.engine.GameMIDlet;
import com.handinfo.engine.GameManager;
import com.handinfo.engine.net.GameProtocol;
import com.handinfo.game.ActorRole;
import com.handinfo.game.GameScene;
import com.handinfo.uimenu.AbsHyperLinker;
import com.handinfo.uimenu.UIAbstractWindow;
import com.handinfo.uimenu.UIListener;
import com.handinfo.uimenu.UIManager;
import com.handinfo.uimenu.UIPopmenu;
import com.handinfo.uimenu.UITools;
import com.handinfo.uimenu.UIWindowFactory;
import com.handinfo.util.Tools;
import com.test.touch.GameTouchManager;
import com.xl.lib.Utils;

/**
 * @author venizeng
 *
 */
public class UIMarket extends UIAbstractWindow {
	private final int STATE_LIST = 1;// 商品列表，选择道具
	private final int STATE_PRESENT = 2;// 赠送界面，选择某人进行赠送

	private String m_title;
	private String[] m_labels;
	private String[][] m_keys;
	private short[][] m_iconIds;
	private String[][] m_names;
	private byte[][] m_pre_des;// 前面的描述
	private byte[][] m_hou_des;// 后面的描述
	private long[][] m_money_rmbs;
	private long[][] m_money_yinpiaos;
	private Object[][] m_deses;
	private final String YUER = "余额：";

	private int H_PRE = AbstractGameScreen.FONT_H << 1;
	//
	private int m_list_index_first;
	private int m_list_index_label;
	private int m_list_index_page;
	private int NUM_LIST_ROWS;// 一页显示多少条道具
	private int NUM_LIST_LABELS = 3;// 一页显示多少条标签

	public UIMarket(int id) {
		super(id);
		setBounds(UITools.X_BG, UITools.Y_BG, UITools.W_BG, UITools.H_BG);

		m_state = STATE_LIST;
		m_index = 0;
	}

	/**
	 * 接收信息 2012-4-18 下午03:01:27 venizeng
	 *
	 * @param orderId
	 * @param dis
	 */
	public static void recvMessage(int orderId, DataInputStream dis) {
		switch (orderId) {
		case GameProtocol.CS_OPEN_MARKET:// 接收商城道具列表
			UIMarket market = (UIMarket) UIManager.getInstance().getWindowById(UIWindowFactory.WINDOW_MARKET);
			if (market != null) {
				return;// 避免重复打开
			}
			market = (UIMarket) UIWindowFactory.createWindow(UIWindowFactory.WINDOW_MARKET);
			try {
				market.recvList(dis);
				market.initDis();
			} catch (IOException e) {
				e.printStackTrace();
				return;
			}
			UIManager.getInstance().addWindow(market);
			break;

		default:
			break;
		}
	}

	/**
	 * 赠送给某人 2012-5-3 上午11:16:54 venizeng
	 *
	 * @param name_recver
	 *            赠送给谁
	 */
	public static void gotoPresent(long id, String name_recver) {
		UIMarket market = (UIMarket) UIManager.getInstance().getWindowById(UIWindowFactory.WINDOW_MARKET);
		if (market == null) {
			return;
		}
		if (market.m_state != market.STATE_PRESENT) {
			return;
		}
		market.m_name_recver = name_recver;
	}

	private void recvList(DataInputStream dis) throws IOException {
		byte state = dis.readByte();
		String info = dis.readUTF();
		if (state != 1) {
			if (info.equals("")) {
				GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, info);
			}
			return;
		}
		int num_label = dis.readInt();// 标签数目
		if (num_label <= 0) {//
			throw new IllegalArgumentException("标签数目小于0 " + num_label);
		}
		m_labels = new String[num_label];
		m_iconIds = new short[num_label][];
		m_names = new String[num_label][];
		m_pre_des = new byte[num_label][];
		m_hou_des = new byte[num_label][];
		m_keys = new String[num_label][];
		m_money_yinpiaos = new long[num_label][];
		m_money_rmbs = new long[num_label][];
		m_deses = new Object[num_label][];
		for (int i = 0; i < num_label; i++) {
			m_labels[i] = dis.readUTF();
			int num_item = dis.readInt();// 相应标签内道具数目
			m_iconIds[i] = new short[num_item];
			m_names[i] = new String[num_item];
			m_pre_des[i] = new byte[num_item];
			m_hou_des[i] = new byte[num_item];
			m_keys[i] = new String[num_item];
			m_money_rmbs[i] = new long[num_item];
			m_money_yinpiaos[i] = new long[num_item];
			m_deses[i] = new Object[num_item][];
			for (int j = 0; j < num_item; j++) {
				m_iconIds[i][j] = dis.readShort();
				m_names[i][j] = dis.readUTF();
				m_pre_des[i][j] = dis.readByte();
				m_hou_des[i][j] = dis.readByte();
				m_keys[i][j] = dis.readUTF();
				m_money_yinpiaos[i][j] = dis.readLong();
				m_money_rmbs[i][j] = dis.readLong();
				// System.out.println("YP = " + m_money_yinpiaos[i][j] +
				// " YB = " + m_money_rmbs[i][j]);
				String temp = dis.readUTF();
				int W_DES = (UITools.IW_RECT_CONTENT<<1) ;
				if (AbsHyperLinker.isHyperLink(temp)) {
					AbsHyperLinker hyperLinker = AbsHyperLinker.createHyperLinker(temp,W_DES, 20);
					hyperLinker.setH(hyperLinker.getRealHeight());
					m_deses[i][j] = hyperLinker;
				} else {
					m_deses[i][j] = Tools.getStrArray(temp, UITools.W_CONTENT - W_DES);
				}
			}
		}
	}

	protected void actionOnIndexChanged() {
		switch (m_state) {
		case STATE_LIST:
			m_list_des_relY = 0;
			break;
		case STATE_PRESENT:
			break;
		}
	}

	protected void actionOnStateChanged() {
		switch (m_state) {
		case STATE_LIST:
			m_title = "商城";
			break;
		case STATE_PRESENT:
			m_title = "赠送";
			break;
		}
	}

	private int getListDataIndex() {
		return m_list_index_page * NUM_LIST_ROWS + m_index;
	}

	protected void keyLogic() {
		switch (m_state) {
		case STATE_LIST:
			logicOnList();
			break;
		case STATE_PRESENT:
			logicOnSend();
			break;
		}
	}

	private void logicListChangePage(int index_page) {
		m_list_index_page = index_page;
		setWindowIndex(0);
		m_list_des_relY = 0;
	}

	private void logicListChangeLabel(int index) {
		if (index == -1) {// 左移动
			m_list_index_label = m_labels.length - 1;
			m_list_index_first = m_labels.length - NUM_LIST_LABELS;
		} else if (index == m_labels.length) {// 有一点
			m_list_index_label = 0;
			m_list_index_first = 0;
		} else {
			m_list_index_label = index;
			if (index < m_list_index_first) {
				m_list_index_first = index;
			} else if (index >= m_list_index_first + NUM_LIST_LABELS) {
				m_list_index_first = index - NUM_LIST_LABELS + 1;
			}
		}
		setWindowIndex(0);
		m_list_des_relY = 0;
		m_list_index_page = 0 ;
	}

	private int logicListGetSumPage() {
		if (m_keys == null) {
			return 0;
		}
		if (m_keys[m_list_index_label] == null) {
			return 0;
		}
		return (m_keys[m_list_index_label].length + NUM_LIST_ROWS - 1) / NUM_LIST_ROWS;
	}

	private int logicListGetCurrentItemNum() {
		if (m_list_index_page == logicListGetSumPage() - 1) {
			return m_keys[m_list_index_label].length - m_list_index_page * NUM_LIST_ROWS;
		}
		return NUM_LIST_ROWS;
	}

	private void logicOnList() {
		if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_UP)) {
			if (m_index == 0) {
				if (m_list_index_page != 0) {
					logicListChangePage(m_list_index_page - 1);
				}
			} else {
				setWindowIndex(m_index - 1);
			}
			m_plan = (m_index+m_list_index_page*NUM_LIST_ROWS)*m_dragH/10;
		} else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_DOWN)) {
			if (m_index == logicListGetCurrentItemNum() - 1) {
				if (m_list_index_page != logicListGetSumPage() - 1) {
					logicListChangePage(m_list_index_page + 1);
				}
			} else {
				setWindowIndex(m_index + 1);
			}
			m_plan = (m_index+m_list_index_page*NUM_LIST_ROWS)*m_dragH/10;
		} else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT)) {
			if (m_list_index_page != 0) {
				logicListChangePage(m_list_index_page - 1);
			}
		} else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT)) {
			if (m_list_index_page != logicListGetSumPage() - 1) {
				logicListChangePage(m_list_index_page + 1);
			}
		} else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_D)) {// *
			logicListChangeLabel(m_list_index_label - 1);
		} else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_B)) {// #
			logicListChangeLabel(m_list_index_label + 1);
		} else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT) || AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
			final UIPopmenu popmenu = (UIPopmenu) UIManager.getInstance().createWindow(UIWindowFactory.WINDOW_POPMENU);
			popmenu.setPopmenus(new String[] { "购买", "查看"});//, "充值", "记录", "返回" });
			popmenu.addListener(new UIListener() {

				public void actionPerformed() {
					if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT) || AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
						switch (popmenu.getCurrentIndex()) {
						case 0:// 购买
							int listIndex = getListDataIndex();
							UIMultiMoney.gotoBuyItem(m_names[m_list_index_label][listIndex], m_keys[m_list_index_label][listIndex],
									m_money_yinpiaos[m_list_index_label][listIndex], m_money_rmbs[m_list_index_label][listIndex]);
							UIMultiMoney.setBuyedIndex(m_index+m_list_index_page*NUM_LIST_ROWS);
							break;
						case 1:// 查看
							String key = m_keys[m_list_index_label][getListDataIndex()];
							GameManager.getInstance().m_game_message.sendActorItemDetail(key);
							break;
//						case 2:// 赠送
//							setWindowStatus(STATE_PRESENT);
//							break;
						case 2:// 充值
							UIChongzhi.gotoChongzhi();
							break;
						case 3:// 记录
							UIChongzhi.gotoRecord();
							break;
						// case 4:// 消费记录
						// UIChongzhi.gotoYuanbaoConsumeRecord();
						// break;
						}
						UIManager.getInstance().removeWindow(popmenu);
					} else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
						UIManager.getInstance().removeWindow(popmenu);
					}
				}

			});
		} else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
			close();
		}
	}

	private void logicOnSend() {
		if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)) {
			UIMultiMoney.gotoPresent(m_names[m_list_index_label][getListDataIndex()], m_keys[m_list_index_label][getListDataIndex()],
					m_money_yinpiaos[m_list_index_label][getListDataIndex()], m_money_rmbs[m_list_index_label][getListDataIndex()], m_id_recver, m_name_recver);
		} else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
			setWindowStatus(STATE_LIST);
		} else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
			if (m_isSelForm) {
				gotoSendSetName();
			} else {
				UISocialWindow.MarketToSocial(0);// 直接找好友
			}
		} else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_UP) || AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_DOWN)) {
			m_isSelForm = !m_isSelForm;
		}
	}

	private void gotoSendSetName() {
		Form myForm = new Form("输入名称");
		//
		final TextField textField1 = new TextField("名称", m_name_recver, ActorRole.NUM_MAX_NAME, TextField.ANY);
		final StringItem warnInfoItem = new StringItem("", "");
		final Command ok = new Command("确定", Command.OK, 1);
		final Command cancel = new Command("返回", Command.CANCEL, 1);
		//
		myForm.append(textField1);
		myForm.append(warnInfoItem);
		myForm.addCommand(ok);
		myForm.addCommand(cancel);
		//
		CommandListener listener = new CommandListener() {

			public void commandAction(Command c, Displayable d) {
				if (c == ok) {
					m_name_recver = textField1.getString();
					// 发送拆分指令
					Display.getDisplay(GameMIDlet.s_instance).setCurrent(GameMIDlet.s_canvas);
				} else if (c == cancel) {
					Display.getDisplay(GameMIDlet.s_instance).setCurrent(GameMIDlet.s_canvas);
				}
			}
		};
		myForm.setCommandListener(listener);
		Display.getDisplay(GameMIDlet.s_instance).setCurrent(myForm);
	}

	protected void paintContent(Graphics g) {
		switch (m_state) {
		case STATE_LIST:
			paintOnList(g);
			break;
		case STATE_PRESENT:
			paintOnList(g);
			paintOnPresent(g);
			break;
		}
	}
	private Image mshowRecord;
	private void paintOnList(Graphics g) {
		paintListBg(g);
//		paintListTitle(g);
//		paintListRect(g);
		paintListItems(g);
		paintListDes(g);

		paintDrag(g);

		if (mshowRecord == null) {//日本人
            try {
                mshowRecord = Image.createImage("/record.png");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (mshowRecord != null) {
            g.drawImage(mshowRecord, 22, 35, Graphics.LEFT|Graphics.TOP);
        }
//		paintListPage(g);
//		paintOwerMoney(g);
	}

	private void paintOwerMoney(Graphics g){
		int tempX = X_LIST_MONEY;
		int tempY = Y_LIST_MONEY;
		UITools.paintMoneyYinpiaoRB(g,  tempX - UITools.W_MONEY_RMB, tempY, GameManager.getInstance().m_role.m_money_yinpiao, false,YUER,Tools.YELLOW, Tools.BLACK);
		UITools.paintMoneyRmbRB(g, tempX, tempY, GameManager.getInstance().m_role.m_money_rmb,false);
		//UITools.paintMoneyGameRB(g, tempX, tempY, GameManager.getInstance().m_role.m_money_game,false);
	}

	private void paintListBg(Graphics g) {
		UITools.paintBackGround(g, m_x, m_y, m_w, m_h, "商城");
	}

	private int Y_LABEL;// 标签和箭头的高度
	private int X_ARR_LEFT;
	private int X_LABEL_FIRST;
	private int X_ARR_RIGHT;
	private int X_LIST_RECT;
	private int Y_LIST_RECT;
	private int Y_LIST_DESC;
	private int H_LITS_DESC;
	private int H_LIST_RECT;
	private int X_LIST_MONEY;
	private int Y_LIST_MONEY;

	final int W_LABEL = AbstractGameScreen.FONT_W * 2 + 4;
	final int RW_LABEL = W_LABEL + UITools.IW_RECTS;
	private int m_list_des_relY;// 偏移量

	private void initDis() {
		NUM_LIST_LABELS = ((UITools.W_RECT - ((UIManager.IMAGE_XINGJING.getWidth() >> 1) << 1) + UITools.IW_RECTS)/(W_LABEL + UITools.IW_RECT_CONTENT)) ;
		NUM_LIST_LABELS = NUM_LIST_LABELS > m_labels.length ? m_labels.length : NUM_LIST_LABELS ;
		X_LABEL_FIRST = ((UITools.W_BG - (NUM_LIST_LABELS * RW_LABEL - UITools.IW_RECTS)) >> 1);
		X_ARR_LEFT = X_LABEL_FIRST - (UIManager.IMAGE_LABEL_ARR.getWidth()) - 2;
		X_ARR_RIGHT = UITools.W_BG - X_ARR_LEFT - (UIManager.IMAGE_LABEL_ARR.getWidth());

		Y_LABEL = UITools.INITY_CONTENT;
		//
		X_LIST_RECT = UITools.INITX_CONTENT;
		Y_LIST_RECT = Y_LABEL + UITools.H_LABEL_STR + UITools.IH_RECTS;
		//
		H_LITS_DESC = AbstractGameScreen.FONT_H * 3 + (UITools.IH_RECT_CONTENT << 1);
		//int h = UITools.H_RECT - (UITools.H_LABEL_STR + UITools.IH_RECTS) - (H_LITS_DESC + UITools.IH_RECTS);
		int h = UITools.H_RECT - (UITools.H_LABEL_STR + H_LITS_DESC + 4*UITools.IH_RECTS + 2);
		H_LIST_RECT = h;
		h -= (UITools.IH_RECT_CONTENT << 1);
		NUM_LIST_ROWS = h / H_PRE;

		Y_LIST_DESC = Y_LIST_RECT + H_LIST_RECT + UITools.IH_RECTS + 10;
		X_LIST_MONEY = X_LIST_RECT + UITools.W_RECT;
		//#if screen == "320240"
		//# Y_LIST_MONEY = Y_LIST_RECT + H_LIST_RECT + 12;
		//#else
		 Y_LIST_MONEY = Y_LIST_RECT + H_LIST_RECT + 27;
		//#endif

		 m_plan = 0;

	}

	private void paintListTitle(Graphics g) {
		int tempY = m_y + Y_LABEL;
		paintListLabelArr(g, m_x + X_ARR_LEFT, tempY, true);
		paintListLabels(g, m_x + X_LABEL_FIRST, tempY);
		paintListLabelArr(g, m_x + X_ARR_RIGHT, tempY, false);
	}

	/**
	 * 2012-4-18 下午03:50:00 venizeng
	 *
	 * @param g
	 * @param i
	 * @param tempY
	 */
	private void paintListLabels(Graphics g, int x, int y) {
		if (m_labels == null) {
			return;
		}
		int tempX = x;
		int tempY = y;
		for (int i = 0; i < NUM_LIST_LABELS; i++) {
			int index = m_list_index_first + i;
			if (index < 0 ) {
				continue ;
			}
			if (index >= m_labels.length) {
				continue;
			}
			if (m_labels[index] == null) {
				continue;
			}
			UITools.paintLabelStr(g, tempX, tempY, W_LABEL, UITools.H_LABEL_STR, m_labels[index], m_list_index_label == index);
			Tools.drawNumber(g, GameScene.s_img_num, tempX, tempY, UITools.W_NUM, UITools.H_NUM, i + 1, 0, Tools.LEFT_TOP, false);
			tempX += RW_LABEL;
		}
	}

	/**
	 * 2012-4-18 下午03:49:57 venizeng
	 *
	 * @param g
	 * @param i
	 * @param tempY
	 * @param b
	 */
	private void paintListLabelArr(Graphics g, int x, int y, boolean isLeft) {
		boolean isLight = true;
		if (isLeft) {
			isLight = true;
		} else {
			isLight = true;
		}
		UITools.paintLabelArr(g, x, y, isLeft, isLight);
	}

	private void paintListRect(Graphics g) {
		int tempX = m_x + X_LIST_RECT;
		int tempY = m_y + Y_LIST_RECT;
		UITools.paintContentRect(g, tempX, tempY, UITools.W_RECT, H_LIST_RECT);
	}

	private void paintListItems(Graphics g) {
		int clipX = g.getClipX();
		int clipY = g.getClipY();
		int clipW = g.getClipWidth();
		int clipH = g.getClipHeight();
		int index_top = m_list_index_page * NUM_LIST_ROWS;
		int tempX = m_x + X_LIST_RECT;
		int tempY = m_y + Y_LIST_RECT;
		g.setClip(tempX, tempY, UITools.W_RECT, H_LIST_RECT - (UITools.IH_RECT_CONTENT << 1));
		tempX += UITools.IW_RECT_CONTENT;
		tempY += UITools.IH_RECT_CONTENT;
		int touchW = 50;
		int w_item = UITools.W_RECT - (UITools.IW_RECT_CONTENT << 1)-touchW;
		int x_fos = 0;
		int y_fos = 0;
		for (int i = 0; i < NUM_LIST_ROWS; i++) {
			int index = index_top + i;
			if (index >= m_keys[m_list_index_label].length) {
				break;
			}
			g.setColor(UIManager.getPaletteColor(UIManager.COLOR_WORD_NOS));
			g.drawRect(tempX, tempY, w_item, H_PRE);
			paintListItem(g, tempX, tempY, w_item, H_PRE, index);
			if (i == m_index) {
				x_fos = tempX;
				y_fos = tempY;
			}
			tempY += H_PRE;
		}
		g.setClip(clipX, clipY, clipW, clipH);
		paintSelBox(g, x_fos, y_fos, w_item, H_PRE);
		// UITools.drawSelectBox(g, x_fos, y_fos, w_item, H_PRE);
	}
	private int m_plan = 0;
	private int m_dragH = 125;
	private int m_dragY = 55;
	private int m_dragW = 30;
	   private void paintDrag(Graphics g)
	    {
//	        if (m_state == STATE_BASIC) {
//	            return;
//	        }
	        UITools.paintScrollBar(g, m_x + UITools.X_DRAG-m_dragW/2, m_y + m_dragY, m_dragH, m_plan);
	    }

	private void paintSelBox(Graphics g, int x, int y, int w, int h) {
		paintSelBlur(g, x, y, w, h);
		int iw = 2;
		paintLanjiao(g, x - iw, y, w + (iw << 1), h);
	}

	public static void paintSelBlur(Graphics g, int x, int y, int w, int h) {
		int cx = g.getClipX();
		int cy = g.getClipY();
		int cw = g.getClipWidth();
		int ch = g.getClipHeight();
		int color = g.getColor();
		g.setClip(0, 0, AbstractGameScreen.SCREEN_WIDTH, AbstractGameScreen.SCREEN_HEIGHT);
		g.setColor(0x4f0000);
		g.drawRect(x - 4, y - 2, w + 7, h + 4);
		g.setColor(0x00ffea);
		g.drawRect(x - 3, y - 1, w + 5, h + 2);
		g.drawRect(x - 2, y, w + 3, h);
		g.drawImage(GameManager.getInstance().img_select, x, y + (h >> 1), Tools.RIGHT_VCENTER);
		Tools.drawRegion(g, GameManager.getInstance().img_select, x + w, y + (h >> 1), Tools.TRANS_H, Tools.LEFT_VCENTER);
		g.setClip(cx, cy, cw, ch);
		g.setColor(color);
	}

	public static void paintLanjiao(Graphics g, int x, int y, int w, int h) {
		Tools.drawRegion(g, UIManager.IMAGE_LANJIAO, x, y, Tools.TRANS_H90, Tools.LEFT_TOP);
		Tools.drawRegion(g, UIManager.IMAGE_LANJIAO, x + w, y, Tools.TRANS_H, Tools.RIGHT_TOP);
		int ih = 1;
		Tools.drawRegion(g, UIManager.IMAGE_LANJIAO, x, y + h + ih, Tools.TRANS_90, Tools.LEFT_BOTTOM);
		Tools.drawRegion(g, UIManager.IMAGE_LANJIAO, x + w, y + h + ih, Tools.TRANS_V90, Tools.RIGHT_BOTTOM);
	}

	/**
	 *
	 * 2012-4-18 下午03:55:23 venizeng
	 *
	 * @param g
	 * @param x
	 * @param y
	 * @param index_data
	 *            当前标签选中的数据索引
	 */
	private void paintListItem(Graphics g, int x, int y, int w, int h, int index_data) {
		if (m_names[m_list_index_label][index_data] == null) {
			return;
		}
		// 画提示1
		int tempX = x;
		int tempY =  y + 1 ;
		int tempW = AbstractGameScreen.FONT_W << 1;
		//
		switch (m_pre_des[m_list_index_label][index_data]) {
		case  1:
			g.setColor(0x4cff6b);
			g.fillTriangle(x, tempY, x, tempY + (H_PRE >> 2) * 3, x + tempW,tempY);
			g.drawImage(UIManager.IMAGE_XIN, x + 3, y +2, 0);
			break ;
		case 2:
			g.setColor(UIManager.getPaletteColor(UIManager.COLOR_WORD_NOS));
			g.fillTriangle(x, tempY, x, tempY + (H_PRE >> 2) * 3, x + tempW,tempY);
			g.drawImage(UIManager.IMAGE_ZHE , x + 3, y + 2, 0);
			break ;
		}
		//
		int iw = 10 ;
		int iww = 5 ;
		int ih = H_PRE - UIManager.IMAGE_JINENGBASE.getHeight() - 2 ;
		int ihh = 6 ;
		tempX = x + iw;
		tempY = y + ih ;
		short id = (short) (m_iconIds[m_list_index_label][index_data] - 1);
		g.drawImage(UIManager.IMAGE_JINENGBASE, tempX, tempY, 0);
		Tools.drawRegion(g, UIManager.IMAGE_JINENGBASE, tempX+14, tempY, Tools.TRANS_H, 0);
		tempX += iww;
		tempY += ihh;
		AbstractGameScreen.drawRegion(g, GameScene.s_img_icon, (id % GameScene.SIZE_IMG_ICON[2]) * GameScene.SIZE_IMG_ICON[0], (id / GameScene.SIZE_IMG_ICON[2])
				* GameScene.SIZE_IMG_ICON[1], GameScene.SIZE_IMG_ICON[0], GameScene.SIZE_IMG_ICON[1], 0,tempX, tempY, 0);

		// 画名字
		g.setColor(UIManager.getPaletteColor(UIManager.COLOR_WORD_YESS));
		g.drawString(m_names[m_list_index_label][index_data], tempX + GameScene.SIZE_IMG_ICON[0] + UITools.IW_RECTS, y, 0);

		tempX = x + w - 1;
		// 画热销
		switch (m_hou_des[m_list_index_label][index_data]) {
		case 1:
			g.drawImage(UIManager.IMAGE_REXIAOZHONG, tempX, y + 1, Tools.RIGHT_TOP);
			break ;
		}
		//
		if (m_money_rmbs[m_list_index_label][index_data] != 0) {
			UITools.paintMoneyRmbRB(g, tempX, y + h, m_money_rmbs[m_list_index_label][index_data], false);
		}
		if (m_money_yinpiaos[m_list_index_label][index_data] != 0) {
			UITools.paintMoneyYinpiaoRB(g, tempX - UITools.W_MONEY_RMB, y + h, m_money_yinpiaos[m_list_index_label][index_data], false);
		}
	}

	private void paintBorder(Graphics g, int x, int y, int w, int h) {
		int iw = 2;
		int ih = 2;
		Tools.drawRegion(g, UIManager.IMAGE_BORDER_CORNER, x + iw, y + ih, Tools.TRANS_HV, Tools.LEFT_TOP);
		Tools.drawRegion(g, UIManager.IMAGE_BORDER_CORNER, x + w - iw + 1, y + ih, Tools.TRANS_90, Tools.RIGHT_TOP);
		Tools.drawRegion(g, UIManager.IMAGE_BORDER_CORNER, x + iw, y + h - ih + 1, Tools.TRANS_H, Tools.LEFT_BOTTOM);
		Tools.drawRegion(g, UIManager.IMAGE_BORDER_CORNER, x + w - iw + 1, y + h - ih + 1, Tools.TRANS_H90, Tools.RIGHT_BOTTOM);
	}

	private void paintListDes(Graphics g) {
		int tempX = m_x + X_LIST_RECT;
		int tempY = m_y + Y_LIST_DESC;
		UITools.paintContentRect(g, tempX, tempY, UITools.W_RECT, H_LITS_DESC);
		// UITools.paintBorderConer(g, tempX, tempY, UITools.W_RECT,
		// H_LITS_DESC);
		paintBorder(g, tempX, tempY, UITools.W_RECT, H_LITS_DESC);

		tempX += UITools.IW_RECT_CONTENT;
		tempY += UITools.IH_RECT_CONTENT;
		int clipX = g.getClipX();
		int clipY = g.getClipY();
		int clipW = g.getClipWidth();
		int clipH = g.getClipHeight();
		int contentH = H_LITS_DESC - (UITools.IH_RECT_CONTENT << 1);
		g.setClip(tempX, tempY, UITools.W_CONTENT, H_LITS_DESC - (UITools.IH_RECT_CONTENT << 1));
		tempX += UITools.IW_RECT_CONTENT;
		if (m_deses != null) {
			if (m_list_index_label < m_deses.length) {
				int dataIndex = getListDataIndex();
				if (dataIndex < m_deses[m_list_index_label].length) {
					int REALH = 0;
					if (m_deses[m_list_index_label][dataIndex] instanceof AbsHyperLinker) {
						AbsHyperLinker hyperLinker = (AbsHyperLinker) m_deses[m_list_index_label][dataIndex];
						hyperLinker.paintInRect(g, tempX, tempY - m_list_des_relY);
						REALH = hyperLinker.getRealHeight();
					} else {
						String[] temps = (String[]) m_deses[m_list_index_label][dataIndex];
						g.setColor(UIManager.getPaletteColor(UIManager.COLOR_WORD_NOS));
						if (temps != null) {
							for (int i = 0; i < temps.length; i++) {
								if (temps[i] != null) {
									g.drawString(temps[i], tempX, tempY - m_list_des_relY, 0);
									tempY += AbstractGameScreen.FONT_H;
									REALH += AbstractGameScreen.FONT_H;
								}
							}
						}
					}
					int H = REALH - contentH;
					if (H > 0) {
						m_list_des_relY++;
						if (m_list_des_relY >= REALH) {
							m_list_des_relY = 0;
						}
					}
				}
			}
		}
		g.setClip(clipX, clipY, clipW, clipH);
	}

	private void paintListPage(Graphics g) {
		UITools.paintPage(g, m_x + (m_w >> 1), m_y + m_h - UITools.IW_WIN, m_list_index_page, logicListGetSumPage());
	}

	private int X_SEND = 20;// 相对list界面的宽度
	private int Y_SEND = 70;
	private int W_SEND = UITools.W_BG - (X_SEND << 1);
	private int H_SEND = UITools.H_BG - (Y_SEND << 1);
	private String m_name_recver = "";
	private long m_id_recver = -1;
	private boolean m_isSelForm = true;

	private void paintOnPresent(Graphics g) {
		paintSendBg(g);
		paintSendContent(g);
	}

	private void paintSendBg(Graphics g) {
		int x = m_x + X_SEND;
		int y = m_y + Y_SEND;
		UITools.drawWindowBackgroud(g, x, y, W_SEND, H_SEND);
		UITools.paintBackgroundTitleOn240320(g, x, y, W_SEND, H_SEND, m_title); // 绘制标题
		UITools.paintBackgroundButton(g, x, y, W_SEND, H_SEND, true, true);
	}

	private void paintSendContent(Graphics g) {
		int initX = 3;
		int tempX = m_x + X_SEND + initX;
		int tempY = m_y + Y_SEND + UITools.INITY_CONTENT;
		g.setColor(UIManager.getPaletteColor(UIManager.COLOR_WORD_YESS));
		g.drawString("赠送给", tempX, tempY, 0);
		tempY += (AbstractGameScreen.FONT_H + UITools.IH_RECTS);

		UITools.paintInputRectLT(g, tempX, tempY, (W_SEND - (initX << 1)), UITools.H_LABEL_STR, m_name_recver, m_isSelForm);
		tempY += (UITools.H_LABEL_STR + UITools.IH_RECTS);
		int w = 100;
		tempX = m_x + X_SEND + ((W_SEND - w) >> 1);
		UITools.paintLabelStr(g, tempX, tempY, w, UITools.H_LABEL_STR, "选择好友", !m_isSelForm);
	}
	private int[][] mupdown, mlisttouch;
	   protected void registerTouch() {
	        super.registerTouch();

	        initTouchRects();
	    }
    public void initTouchRects() {
        clearAllRects();
        registerNormalSoft();
//        if (mupdown == null) {
            mupdown = new int[][]{
                    {420,60,40,40},
                    {420,170,40,40},
                    {20,35,70,20}
            };
            initTouchRect(mupdown);
//        }
//        if (mlisttouch == null) {
            mlisttouch = new int[NUM_LIST_ROWS][];
            int index_top = m_list_index_page * NUM_LIST_ROWS;
            int tempX = m_x + X_LIST_RECT;
            int tempY = m_y + Y_LIST_RECT;
            tempX += UITools.IW_RECT_CONTENT;
            tempY += UITools.IH_RECT_CONTENT;
            int touchW = 50;
            int w_item = UITools.W_RECT - (UITools.IW_RECT_CONTENT << 1)-touchW;
//            int x_fos = 0;
//            int y_fos = 0;
            for (int i = 0; i < NUM_LIST_ROWS; i++) {
                int index = index_top + i;
                if (index >= m_keys[m_list_index_label].length) {
                    break;
                }
                mlisttouch[i] = new int[]{tempX, tempY, w_item, H_PRE};
//                g.setColor(UIManager.getPaletteColor(UIManager.COLOR_WORD_NOS));
//                g.drawRect(tempX, tempY, w_item, H_PRE);
//                paintListItem(g, tempX, tempY, w_item, H_PRE, index);
//                if (i == m_index) {
//                    x_fos = tempX;
//                    y_fos = tempY;
//                }
                tempY += H_PRE;
            }
            initTouchRect(mlisttouch);
//        }
    }
    static final String RECORD_URL = "http://58.215.185.99:18001/DTgame_HTTP/http_queryHistory?roleId=";
private AlertDialog mAlertDialog;
    @Override
    protected void onTouchRect(int[][] rect, int touchid) {
        if (rect == mupdown) {
            if (touchid == 0) {
                GameTouchManager.keyPressedDir(1);
            } else if (touchid == 1){
                GameTouchManager.keyPressedDir(3);
            } else {//日本人
                if (mAlertDialog == null) {
                    mAlertDialog = new AlertDialog.Builder(Utils.getContext()).create();
                    mAlertDialog.setTitle("消费记录");
                }
                if (!mAlertDialog.isShowing()) {
                    mAlertDialog.setMessage("读取数据中，请稍候...");
                    mAlertDialog.show();
                }
              //访问记录
                String recorde = "暂无消费";
                try {
                    HttpConnection http = (HttpConnection)Connector.open(RECORD_URL+GameManager.getInstance().m_role.m_gameID);
                    DataInputStream dis = http.openDataInputStream();
                    byte[] array = UIMultiMoney.getByteFromInputstream(dis);
                    String result = new String(array);
                    Log.d("xltest", "get id result is ="+result);
                    if (result != null && !result.trim().equals("")) {
                        recorde = parseRecord(result);
                    }
                    http.close();
                    dis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

//                if (mAlertDialog.isShowing()) {
                    mAlertDialog.setMessage(recorde);
                    mAlertDialog.show();
//                }
//                new AlertDialog.Builder(Utils.getContext())
//                .setTitle("消费记录")
//                .setMessage(recorde)
//                .setNegativeButton("返回", null)
//                .show();
            }
        } else if (rect == mlisttouch) {
            if (touchid != m_index) {
                m_index = touchid;
            } else {
                GameTouchManager.keyPressedFire();
            }
        }
    }

    private String parseRecord(String result) {
        return result;
    }


}
